package org.javaboy.producer01.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 主题交换机
 */
@Configuration
public class TopicConfig {

    // 主题交换机
    public static final String TOPIC_EXCHANGE = "topic.exchange";

    public static final String XIAOMI_QUEUE = "xiaomi_queue";
    public static final String HUAWEI_QUEUE = "huawei_queue";
    public static final String PHONE_QUEUE = "phone_queue";

    @Bean
    Binding xiaomiBinding() {
        return BindingBuilder.bind(xiaomiQueue()).to(topicExchange())
                .with("xiaomi.#");
        //这样写，相当于是直连交换机
//                .with(XIAOMI_QUEUE);
    }

    @Bean
    Binding huaweiBinding() {
        return BindingBuilder.bind(huaweiQueue()).to(topicExchange())
                .with("huawei.#");
    }

    @Bean
    Binding phoneBinding() {
        return BindingBuilder.bind(phoneQueue()).to(topicExchange())
                .with("#.phone.#");
    }

    @Bean
    Queue huaweiQueue() {
        return new Queue(HUAWEI_QUEUE, true, false, false);
    }

    @Bean
    Queue phoneQueue() {
        return new Queue(PHONE_QUEUE, true, false, false);
    }

    @Bean
    Queue xiaomiQueue() {
        return new Queue(XIAOMI_QUEUE, true, false, false);
    }

    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE, true, false);
    }
}
